/*
 * Copyright 2005-2013 finalist.cn. All rights reserved.
 * Support: http://www.finalist.cn
 * License: http://www.finalist.cn/license
 */
package com.finalist.entity;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.PreRemove;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * Entity - 管理员
 * 
 * @author Finalist IT Group
 * @version 3.0
 */
@Entity
@Table(name = "xx_admin")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_admin_sequence")
public class Admin extends BaseEntity {

	private static final long serialVersionUID = -7519486823153844426L;

	/** 用户名 */
	private String username;

	/** 密码 */
	private String password;

	/** E-mail */
	private String email;

	/** 姓名 */
	private String name;

	/** 部门 */
	private String department;

	

	/** 是否锁定 */
	private Boolean isLocked;

	/** 连续登录失败次数 */
	private Integer loginFailureCount;

	/** 锁定日期 */
	private Date lockedDate;

	/** 最后登录日期 */
	private Date loginDate;

	/** 最后登录IP */
	private String loginIp;

	/** 角色 */
	private Set<Role> roles = new HashSet<Role>();

	/** 订单 */
	private Set<Order> orders = new HashSet<Order>();
	
	/** 商品*/
	private Set<Product> products= new HashSet<Product>();
	
	
	
	private boolean hasCheck =false;
	
	/** 申请单位*/
	private String company;
	
	/** 身份证号*/
	private String idNum;
	
	/** 证件号*/
	private String paperNum;
	
	/** 手持身份证照片*/
	private String idHandsImg;
	
	/** 身份证正面*/
	private String idFrontImg;
	
	/** 身份证背面*/
	private String idBackImg;
	
	/** 联系地址*/
	private String address;
	
	/** 联系电话*/
	private String phoneNum;
	
	/** 审核是否通过了 */
	private Boolean isEnabled;
	
	private Integer type ; //申请类别，1 个人， 2企业
	
	private Integer step = 1;//注册程序进入到了第几步 1,2,3,4
	
	private Boolean isCheck  = false; //审核是否通过
	
	private String reason ;//审核不通过原因
	
	private String ticket; //用于邮箱激活
	
	private boolean hasEmailAction = false;
	private Date registerTime; //邮箱激活时间
	
	/** 公司公章图片*/
	private String gzImg;
	
	private String companyName ;
	private String companyCode ;
	
	
	/**合作身份者ID*/
	private String  partner;
	/**卖家支付宝账户*/
	private String selleEmail;
	/**交易安全检验码，由数字和字母组成的32位字符串*/
	private String safeKey;
	
	private String mchId;
	private String paternerKey;
	
	/**
	 * 服务提供商
	 */
	private String serviceProvider;
	
	
	
	public String getServiceProvider() {
		return serviceProvider;
	}

	public void setServiceProvider(String serviceProvider) {
		this.serviceProvider = serviceProvider;
	}

	public String getPartner() {
		return partner;
	}

	public void setPartner(String partner) {
		this.partner = partner;
	}

	public String getSelleEmail() {
		return selleEmail;
	}

	public void setSelleEmail(String selleEmail) {
		this.selleEmail = selleEmail;
	}

	

	public String getMchId() {
		return mchId;
	}

	public String getSafeKey() {
		return safeKey;
	}

	public void setSafeKey(String safeKey) {
		this.safeKey = safeKey;
	}

	public void setMchId(String mchId) {
		this.mchId = mchId;
	}

	public String getPaternerKey() {
		return paternerKey;
	}

	public void setPaternerKey(String paternerKey) {
		this.paternerKey = paternerKey;
	}

	@Length(max = 100)
	@Column(nullable = false)
	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	@Length(max = 18)
	@Column(nullable = false)
	public String getIdNum() {
		return idNum;
	}

	public void setIdNum(String idNum) {
		this.idNum = idNum;
	}

	@Length(max = 20)
	@Column(nullable = false)
	public String getPaperNum() {
		return paperNum;
	}

	public void setPaperNum(String paperNum) {
		this.paperNum = paperNum;
	}

	@Length(max = 250)
	@Column(nullable = false)
	public String getIdHandsImg() {
		return idHandsImg;
	}

	public void setIdHandsImg(String idHandsImg) {
		this.idHandsImg = idHandsImg;
	}

	@Length(max = 250)
	@Column(nullable = false)
	public String getIdFrontImg() {
		return idFrontImg;
	}

	public void setIdFrontImg(String idFrontImg) {
		this.idFrontImg = idFrontImg;
	}

	@Length(max = 250)
	@Column(nullable = false)
	public String getIdBackImg() {
		return idBackImg;
	}

	public void setIdBackImg(String idBackImg) {
		this.idBackImg = idBackImg;
	}

	@Length(max = 250)
	@Column(nullable = false)
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Length(max = 20)
	@Column(nullable = false)
	public String getPhoneNum() {
		return phoneNum;
	}

	public void setPhoneNum(String phoneNum) {
		this.phoneNum = phoneNum;
	}


	@OneToMany(mappedBy = "admin", fetch = FetchType.LAZY)
	public Set<Product> getProducts() {
		return products;
	}

	public void setProducts(Set<Product> products) {
		this.products = products;
	}
	
	
	public boolean isHasCheck() {
		return hasCheck;
	}

	public void setHasCheck(boolean hasCheck) {
		this.hasCheck = hasCheck;
	}

	/**
	 * 获取用户名
	 * 
	 * @return 用户名
	 */
	@NotEmpty(groups = Save.class)
	@Pattern(regexp = "^[0-9a-z_A-Z\\u4e00-\\u9fa5]+$")
	@Length(min = 2, max = 20)
	@Column(nullable = false, updatable = false, unique = true, length = 100)
	public String getUsername() {
		return username;
	}

	/**
	 * 设置用户名
	 * 
	 * @param username
	 *            用户名
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * 获取密码
	 * 
	 * @return 密码
	 */
	@NotEmpty(groups = Save.class)
	@Pattern(regexp = "^[^\\s&\"<>]+$")
	@Length(min = 4, max = 20)
	@Column(nullable = false)
	public String getPassword() {
		return password;
	}

	/**
	 * 设置密码
	 * 
	 * @param password
	 *            密码
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * 获取E-mail
	 * 
	 * @return E-mail
	 */
	@NotEmpty
	@Email
	@Length(max = 200)
	@Column(nullable = false)
	public String getEmail() {
		return email;
	}

	/**
	 * 设置E-mail
	 * 
	 * @param email
	 *            E-mail
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * 获取姓名
	 * 
	 * @return 姓名
	 */
	@Length(max = 200)
	public String getName() {
		return name;
	}

	/**
	 * 设置姓名
	 * 
	 * @param name
	 *            姓名
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 获取部门
	 * 
	 * @return 部门
	 */
	@Length(max = 200)
	public String getDepartment() {
		return department;
	}

	/**
	 * 设置部门
	 * 
	 * @param department
	 *            部门
	 */
	public void setDepartment(String department) {
		this.department = department;
	}

	/**
	 * 获取是否启用
	 * 
	 * @return 是否启用
	 */
	@NotNull
	@Column(nullable = false)
	public Boolean getIsEnabled() {
		return isEnabled;
	}

	/**
	 * 设置是否启用
	 * 
	 * @param isEnabled
	 *            是否启用
	 */
	public void setIsEnabled(Boolean isEnabled) {
		this.isEnabled = isEnabled;
	}

	/**
	 * 获取是否锁定
	 * 
	 * @return 是否锁定
	 */
	@Column(nullable = false)
	public Boolean getIsLocked() {
		return isLocked;
	}

	/**
	 * 设置是否锁定
	 * 
	 * @param isLocked
	 *            是否锁定
	 */
	public void setIsLocked(Boolean isLocked) {
		this.isLocked = isLocked;
	}

	/**
	 * 获取连续登录失败次数
	 * 
	 * @return 连续登录失败次数
	 */
	@Column(nullable = false)
	public Integer getLoginFailureCount() {
		return loginFailureCount;
	}

	/**
	 * 设置连续登录失败次数
	 * 
	 * @param loginFailureCount
	 *            连续登录失败次数
	 */
	public void setLoginFailureCount(Integer loginFailureCount) {
		this.loginFailureCount = loginFailureCount;
	}

	/**
	 * 获取锁定日期
	 * 
	 * @return 锁定日期
	 */
	public Date getLockedDate() {
		return lockedDate;
	}

	/**
	 * 设置锁定日期
	 * 
	 * @param lockedDate
	 *            锁定日期
	 */
	public void setLockedDate(Date lockedDate) {
		this.lockedDate = lockedDate;
	}

	/**
	 * 获取最后登录日期
	 * 
	 * @return 最后登录日期
	 */
	public Date getLoginDate() {
		return loginDate;
	}

	/**
	 * 设置最后登录日期
	 * 
	 * @param loginDate
	 *            最后登录日期
	 */
	public void setLoginDate(Date loginDate) {
		this.loginDate = loginDate;
	}

	/**
	 * 获取最后登录IP
	 * 
	 * @return 最后登录IP
	 */
	public String getLoginIp() {
		return loginIp;
	}

	/**
	 * 设置最后登录IP
	 * 
	 * @param loginIp
	 *            最后登录IP
	 */
	public void setLoginIp(String loginIp) {
		this.loginIp = loginIp;
	}

	/**
	 * 获取角色
	 * 
	 * @return 角色
	 */
	@NotEmpty
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "xx_admin_role")
	public Set<Role> getRoles() {
		return roles;
	}

	/**
	 * 设置角色
	 * 
	 * @param roles
	 *            角色
	 */
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	/**
	 * 获取订单
	 * 
	 * @return 订单
	 */
	@OneToMany(mappedBy = "operator", fetch = FetchType.LAZY)
	public Set<Order> getOrders() {
		return orders;
	}

	/**
	 * 设置订单
	 * 
	 * @param orders
	 *            订单
	 */
	public void setOrders(Set<Order> orders) {
		this.orders = orders;
	}

	/**
	 * 删除前处理
	 */
	@PreRemove
	public void preRemove() {
		Set<Order> orders = getOrders();
		if (orders != null) {
			for (Order order : orders) {
				order.setLockExpire(null);
				order.setOperator(null);
			}
		}
	}
	
	 @Transient
	 public Long getAminId(){
		 for (Iterator<Role> iterator = this.getRoles()
					.iterator(); iterator.hasNext();) {
	        	Role role=iterator.next();
	        	if(role!=null&&role.getId()==1){
	        		return null;
	        	}
	      }
		return this.getId();
	 }
	 
	@JsonProperty
	public Integer getStep() {
		return step;
	}

	public void setStep(Integer step) {
		this.step = step;
	}
	
	@JsonProperty
	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public boolean isHasEmailAction() {
		return hasEmailAction;
	}

	public void setHasEmailAction(boolean hasEmailAction) {
		this.hasEmailAction = hasEmailAction;
	}
	
	@JsonProperty
	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public Boolean getIsCheck() {
		return isCheck;
	}

	public void setIsCheck(Boolean isCheck) {
		this.isCheck = isCheck;
	}

	public String getGzImg() {
		return gzImg;
	}

	public void setGzImg(String gzImg) {
		this.gzImg = gzImg;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public String getCompanyCode() {
		return companyCode;
	}

	public void setCompanyCode(String companyCode) {
		this.companyCode = companyCode;
	}

	public String getTicket() {
		return ticket;
	}

	public void setTicket(String ticket) {
		this.ticket = ticket;
	}

	public Date getRegisterTime() {
		return registerTime;
	}

	public void setRegisterTime(Date registerTime) {
		this.registerTime = registerTime;
	}
	
	/** 是否拥有设置上架功能 */
	private Boolean canSetMarketable = false;
	
	/**
	 * 获取是否拥有设置上架功能
	 * 
	 * @return 是否上架拥有设置上架功能
	 */
	/*@Column(nullable = false)*/
	public Boolean getCanSetMarketable() {
		return canSetMarketable;
	}

	/**
	 * 设置是否拥有设置上架功能
	 * 
	 * @param canSetMarketable
	 *            是否拥有设置上架功能
	 */
	public void setCanSetMarketable(Boolean canSetMarketable) {
		this.canSetMarketable = canSetMarketable;
	}
	
}